<template>
  <el-main>
    <p></p>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="search"
          placeholder="搜索电费梯度"
          clearable
          @input="fetchRates"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <p></p>
    <el-table :data="tableData" class="table" style="width: 99%" border>
      <el-table-column label="ID">
        <template #default="scope">
          <span>{{ scope.row.elcId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="阶梯">
        <template #default="scope">
          <span>{{ scope.row.step }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下限(度)">
        <template #default="scope">
          <span>{{ scope.row.down }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上限(度)">
        <template #default="scope">
          <span>{{ scope.row.up }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价(¥)">
        <template #default="scope">
          <span>{{ scope.row.pri }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon><Timer /></el-icon>
            <span style="margin-left: 10px">{{ formatDate(scope.row.creDate) }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <p></p>
    <div style="margin-top: 10px">
      <el-config-provider :locale="locale">
        <el-col span="8" :offset="8">
          <el-pagination
            @current-change="handlePageChange"
            :current-page="currentPage"
            :page-size="pageSize"
            layout="prev, pager, next, jumper"
            :total="total"
          />
        </el-col>
      </el-config-provider>
    </div>
  </el-main>
</template>

<script lang="ts" setup>
import { reactive, ref, computed, onMounted } from "vue";
import { Timer } from "@element-plus/icons-vue";
import zhCn from "element-plus/dist/locale/zh-cn.mjs";
import en from "element-plus/dist/locale/en.mjs";
import apiClient from "@/api"; // 引入 Axios 实例
import dayjs from "dayjs"; // 引入 dayjs

const language = ref("zh-cn");
const locale = computed(() => (language.value === "zh-cn" ? zhCn : en));

const toggle = () => {
  language.value = language.value === "zh-cn" ? "en" : "zh-cn";
};

interface User {
  elcId: number;
  step: string;
  down: number;
  up: number;
  pri: number;
  creDate: string;
}

const tableData = ref<User[]>([]);
const total = ref(0);
const currentPage = ref(1);
const pageSize = ref(5);

const search = ref("");

const fetchRates = async () => {
  try {
    const response = await apiClient.get("/elcPri", {
      params: { page: currentPage.value, size: pageSize.value, search: search.value },
    });
    tableData.value = response.data.records;
    total.value = response.data.total;
  } catch (error) {
    console.error(error);
  }
};

const handlePageChange = async (page: number) => {
  currentPage.value = page;
  await fetchRates();
};

const formatDate = (date: string) => {
  return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
};

onMounted(() => {
  fetchRates();
});
</script>

<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
